import cv2

img = cv2.imread('1.jpg')
## 像素值大小超过阈值取最大值 否则取0
ret,thresh1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
## 和THRESH_BINARY相反
ret,thresh2 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
## 大于阈值的部分设置为阀值，否则不变
ret,thresh3 = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC)
## 大于阈值的部分不变，否则设置为0
ret,thresh4 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO)
## 和THRESH_TOZERO相反
ret,thresh5 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV)
cv2.imshow('thresh1', thresh1)
cv2.imshow('thresh2', thresh2)
cv2.imshow('thresh3', thresh3)
cv2.imshow('thresh4', thresh4)
cv2.imshow('thresh5', thresh5)

cv2.waitKey(0)
cv2.destroyAllWindows()